/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package wave;

import ast.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
import base.*;
import trackmsg.EchoMsg;
import trackmsg.TrackCtrlMsg;
/**
 *
 * @author THAI
 */
public class ASTProductExecution extends WaveExecution {
    public ASTProductExecution(WaveHeadParser parser, WaveEnvironment waveEnv) {
        super(parser, waveEnv);
    }

    public void execute(ASTProduct multi, String tail) {
        SimpleNode left = (SimpleNode)multi.jjtGetChild(0);
        SimpleNode right = (SimpleNode)multi.jjtGetChild(1);

        Utilities.WPPrint(left.toString());
        Utilities.WPPrint(right.toString());

        if (left instanceof ASTFrontalVar) {
            Hashtable fTab = this.waveEnv.getFrontalTable();
            Vector v_left = (Vector)fTab.get(left.getString());

            if (right instanceof ASTFrontalVar) {
                Vector v_right = (Vector)fTab.get(right.getString());

                fTab.put(left.getString(), WaveActs.multiply(v_left, v_right));
                Utilities.WPPrint(fTab);
            }
            else if (right instanceof ASTNodalVar) {
                Hashtable nTab = this.kn.getKNNode(this.waveEnv.getCONTENT()).
                        getNodalTable(waveEnv.getColor());

                Vector v_right = (Vector)nTab.get(right.getString());

                fTab.put(left.getString(), WaveActs.multiply(v_left, v_right));
                //Utilities.WPPrint(v_right);
                Utilities.WPPrint(fTab);
            }
            else if (right instanceof ASTNone) {
                Utilities.WPPrint(fTab);
            }
            else {
                StringTokenizer strTk = new StringTokenizer(right.getString());
                Vector v_right = new Vector();
                while (strTk.hasMoreTokens()) {
                    String temp = strTk.nextToken(";");
                    if (isNum(temp) == true)
                        v_right.add(Integer.parseInt(temp));
                    else
                        v_right.add(temp);
                }

                fTab.put(left.getString(), WaveActs.multiply(v_left, v_right));
                Utilities.WPPrint(fTab);
            }
        }
        else if (left instanceof ASTNodalVar) {
            Hashtable nTab = this.kn.getKNNode(this.waveEnv.getCONTENT()).
                    getNodalTable(waveEnv.getColor());
            Vector v_left = (Vector)nTab.get(left.getString());

            if (right instanceof ASTFrontalVar) {
                Hashtable fTab = this.waveEnv.getFrontalTable();
                Vector v_right = (Vector)fTab.get(right.getString());

                nTab.put(left.getString(), WaveActs.multiply(v_left, v_right));
                Utilities.WPPrint(nTab);
            }
            else if (right instanceof ASTNodalVar) {
                Vector v_right = (Vector)nTab.get(right.getString());

                nTab.put(left.getString(), WaveActs.multiply(v_left, v_right));
                Utilities.WPPrint(nTab);
            }
            else if (right instanceof ASTNone) {
                Utilities.WPPrint(nTab);
            }
            else {
                StringTokenizer strTk = new StringTokenizer(right.getString());
                Vector v_right = new Vector();
                while (strTk.hasMoreTokens()) {
                    String temp = strTk.nextToken(";");
                    if (isNum(temp) == true)
                        v_right.add(Integer.parseInt(temp));
                    else
                        v_right.add(temp);
                }

                nTab.put(left.getString(), WaveActs.multiply(v_left, v_right));
                Utilities.WPPrint(nTab);
            }
        }

        if (waveEnv.getTrackAddress() != null && tail.equals("")) {
            //Neu tail ma rong thi gui echo la TRUE di
            TrackNode fromTrack = wi.getTrackForest().getTrackNode(waveEnv.getTrackAddress());
            TrackNode.Echo echo = TrackNode.Echo.TRUE;
            //Tao message echo va gui vao TrackQueue
            //Utilities.WPPrint("ME");
            TrackCtrlMsg msg = new EchoMsg(fromTrack, echo, waveEnv);
            wi.getTrackQueue().incoming(msg);
        } else if(!tail.equals("")){
            Wave newWave = new Wave(tail, new WaveEnvironment(waveEnv));
            waveQueue.incomingFront(newWave);
        }
        waveEnv.dump();

        Utilities.WPPrint("Executed Assign Product!");
    }

    public boolean isNum (String str) {
        try{
            Integer.parseInt(str);
        } catch(NumberFormatException nfe) {
            return false;
        }
        return true;
    }
}
